package org.mbds.sensorsdatacollector.android.activity;

import java.util.ArrayList;

import org.mbds.sensorsdatacollector.android.R;
import org.mbds.sensorsdatacollector.android.chart.LineChartAttributes;
import org.mbds.sensorsdatacollector.android.chart.PathAttributes;
import org.mbds.sensorsdatacollector.android.chart.PathOnChart;
import org.mbds.sensorsdatacollector.android.db.SensorData;
import org.mbds.sensorsdatacollector.android.geometry.PointFloat;
import org.mbds.sensorsdatacollector.android.tools.SensorObserverInterface;
import org.mbds.sensorsdatacollector.android.tools.SensorsDataManager;
import org.mbds.sensorsdatacollector.android.view.SensorLinesChartView;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SensorChartActivity extends Activity implements SensorObserverInterface {

	private ArrayList<PointFloat> pointsX = new ArrayList<PointFloat>();
	private ArrayList<PointFloat> pointsY = new ArrayList<PointFloat>();
	private ArrayList<PointFloat> pointsZ = new ArrayList<PointFloat>();
	private ArrayList<PointFloat> pointsA = new ArrayList<PointFloat>();
	private ArrayList<float[]> orientations = new ArrayList<float[]>();
	private ArrayList<float[]> rotations = new ArrayList<float[]>();
	private ArrayList<PathOnChart> paths = new ArrayList<PathOnChart>();
	private SensorLinesChartView view;
	private int filter;
	private int sensor;
	private TextView title;
	public final static String TAG = "SensorChartActivity";
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		float firstT = 0;
		for (int i = 0; i < SensorsDataManager.accelerometerData.getSensorDatas().size(); i++) {
			SensorData data = SensorsDataManager.accelerometerData.getSensorDatas().get(i);
			if (i==0) {
				firstT = data.time;
			}
			float t = (float) ((data.time-firstT)
					* SensorsDataManager.MULT_NANO);
			pointsX.add(new PointFloat(t, data.filteredVal[0]));
			pointsY.add(new PointFloat(t, data.filteredVal[1]));
			pointsZ.add(new PointFloat(t, data.filteredVal[2]));
			pointsA.add(new PointFloat(t, (float) data.computedvAL));
			orientations.add(data.orientation);
			rotations.add(data.rotation);
		}
		
		PathAttributes pathAttributes1 = new PathAttributes();
		pathAttributes1.setPointColor("#00AAAAAA");
		pathAttributes1.setPathColor("#FFAF00");
		PathOnChart path1 = new PathOnChart(pointsX, pathAttributes1);

		PathAttributes pathAttributes2 = new PathAttributes();
		pathAttributes2.setPointColor("#00AAAAAA");
		pathAttributes2.setPathColor("#1000ED");
		PathOnChart path2 = new PathOnChart(pointsY, pathAttributes2);

		PathAttributes pathAttributes3 = new PathAttributes();
		pathAttributes3.setPointColor("#00AAAAAA");
		pathAttributes3.setPathColor("#008000");
		PathOnChart path3 = new PathOnChart(pointsZ, pathAttributes3);

		PathAttributes pathAttributes4 = new PathAttributes();
		pathAttributes4.setPointColor("#00AAAAAA");
		pathAttributes4.setPathColor("#FF0000");
		PathOnChart path4 = new PathOnChart(pointsA, pathAttributes4);

		paths.add(path1);
		paths.add(path2);
		paths.add(path3);
		paths.add(path4);
		
		LineChartAttributes lineChartAttributes = new LineChartAttributes(
				getApplicationContext().getString(R.string.axis_x), 
				getApplicationContext().getString(R.string.axis_y));
		lineChartAttributes.setBackgroundColor("#aaabbb");	
		
		view = new SensorLinesChartView(this, paths, lineChartAttributes, orientations, rotations);
		LinearLayout ly = new LinearLayout(this);         
		ly.post(new Runnable() {             
			public void run() {                 
				Rect rect = new Rect();                 
				Window window = getWindow();                 
				window.getDecorView().getWindowVisibleDisplayFrame(rect);                 
				int statusBarHeight = rect.top;                 
				int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop();                 
				int titleBarHeight = contentViewTop - statusBarHeight;                 
				view.setTitleSize(titleBarHeight);
			}         
		}); 

		setContentView(view);
		sensor = SensorsDataManager.accelerometerData.getSensorType();
	}
	
	
	@Override
	public void onPause() {
		SensorsDataManager.accelerometerData.removeObserver(this);
		super.onPause();
	}
	
	@Override
	public void onResume() {
		SensorsDataManager.accelerometerData.addObserver(this);
		super.onResume();
	}
	
	

	public void updateSensorData(ArrayList<SensorData> datas,
			double lastDeltaTime, SensorData lastValues) {
		pointsX.clear();
		pointsY.clear();
		pointsZ.clear();
		pointsA.clear();
		orientations.clear();
		rotations.clear();
		
		float firstT = 0;
		for (int i = 0; i < datas.size(); i++) {
			if (i==0) {
				firstT = datas.get(i).time;
			}
			float t = (float) ((datas.get(i).time-firstT)/1000000000.0);
			pointsX.add(new PointFloat(t, datas.get(i).filteredVal[0]));
			pointsY.add(new PointFloat(t, datas.get(i).filteredVal[1]));
			pointsZ.add(new PointFloat(t, datas.get(i).filteredVal[2]));
			pointsA.add(new PointFloat(t, (float) datas.get(i).computedvAL));
			orientations.add(datas.get(i).orientation);
			rotations.add(datas.get(i).rotation);
		}
		view.configurePaths(paths, orientations, rotations, true);
		view.invalidate();
		
	}
}
